package com.ruoyi.qms.controller;

import java.math.BigDecimal;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.qms.domain.QmsQc;
import com.ruoyi.qms.service.IQmsQcService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 质量检查Controller
 * 
 * @author ruoyi
 * @date 2021-11-04
 */
@RestController
@RequestMapping("/qms/qc")
public class QmsQcController extends BaseController
{
    @Autowired
    private IQmsQcService qmsQcService;

    /**
     * 查询质量检查列表
     */
    @PreAuthorize(hasPermi = "qms:qc:list")
    @GetMapping("/list")
    public TableDataInfo list(QmsQc qmsQc)
    {
        startPage();
        List<QmsQc> list = qmsQcService.selectQmsQcList(qmsQc);
        return getDataTable(list);
    }

    /**
     * 导出质量检查列表
     */
    @PreAuthorize(hasPermi = "qms:qc:export")
    @Log(title = "质量检查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsQc qmsQc) throws IOException
    {
        List<QmsQc> list = qmsQcService.selectQmsQcList(qmsQc);
        ExcelUtil<QmsQc> util = new ExcelUtil<QmsQc>(QmsQc.class);
        util.exportExcel(response, list, "质量检查数据");
    }

    /**
     * 获取质量检查详细信息
     */
    @PreAuthorize(hasPermi = "qms:qc:query")
    @GetMapping(value = "/{qcUuid}")
    public AjaxResult getInfo(@PathVariable("qcUuid") String qcUuid)
    {
        return AjaxResult.success(qmsQcService.selectQmsQcByQcUuid(qcUuid));
    }

    /**
     * 新增质量检查
     */
    @PreAuthorize(hasPermi = "qms:qc:add")
    @Log(title = "质量检查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QmsQc qmsQc) {
        QmsQc q = new QmsQc();
        q.setDocNum(qmsQc.getDocNum());
        List<QmsQc> listByDocNum = qmsQcService.selectQmsQcList(q);
        if (listByDocNum.size() > 0) {
            return new AjaxResult(1521, "您提交的质检号已存在！请重新填写！");
        } else {
            // 获取当前填写的加工数量
            BigDecimal curQty = qmsQc.getQcQuantity();
            // 获取剩余可填写的加工数量
            BigDecimal remainQty = qmsQcService.selectRemainQcQty(qmsQc);
            // 剩余可填写的加工数量和当前填写的加工数量做比较
            if (curQty.compareTo(remainQty) > 0) {
                return new AjaxResult(1521, "您提交的质检数量为" + curQty + "," + "当前剩余实际可质检数量为" + remainQty + "!");
            } else {
                return toAjax(qmsQcService.insertQmsQc(qmsQc));
            }
        }
    }
    /**
     * 修改质量检查
     */
    @PreAuthorize(hasPermi = "qms:qc:edit")
    @Log(title = "质量检查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QmsQc qmsQc)
    {
        return toAjax(qmsQcService.updateQmsQc(qmsQc));
    }

    /**
     * 删除质量检查
     */
    @PreAuthorize(hasPermi = "qms:qc:remove")
    @Log(title = "质量检查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{qcUuids}")
    public AjaxResult remove(@PathVariable String[] qcUuids)
    {
        return toAjax(qmsQcService.deleteQmsQcByQcUuids(qcUuids));
    }
}
